//
//  LoginView.m
//  LoginTest
//
//  Created by 战立军 on 2017/2/20.
//  Copyright © 2017年 战立军. All rights reserved.
//

#import "LoginView.h"

@interface LoginView ()<UITextFieldDelegate,UIActionSheetDelegate>
{
    UIImageView *_logoimageView;//logo
    UITextField *_userName;//用户名
    UITextField *_userPwd;//密码
    
    UIButton *_loginButton;
    UIButton *_registButton;
    UIButton *_forgetButton;
    
}
@end

@implementation LoginView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.colors = @[(__bridge id)[UIUTils colorFromHexCode:@"0489c2"].CGColor,(__bridge id)[UIUTils colorFromHexCode:@"009c93"].CGColor];
        gradientLayer.locations = @[@0.3,@1.0];
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(0, 1.0);
        gradientLayer.frame = self.bounds;
        [self.layer insertSublayer:gradientLayer atIndex:0];
        
//        [self setBackgroundColor:[UIColor yellowColor]];
        
        [self addContentView];
    }
    
    return self;
}

- (void)addContentView
{
    CGFloat heigth = 50;
    
    if(IS_IPHONE_5)
    {
        heigth = 40;
    }
    //pwd
    UIView *usepwdView = [[UIView alloc] initWithFrame:CGRectMake(20, self.center.y-10-heigth, [UIUTils getWindowWidth]-20*2, heigth)];
    usepwdView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.3];
    usepwdView.layer.cornerRadius = heigth/2.0;
    UIButton *pwdbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    pwdbtn.frame = CGRectMake(12, 10, heigth-10*2, heigth-10*2);
    pwdbtn.tag = 101;
    [pwdbtn setBackgroundImage:[UIImage imageNamed:@"password"] forState:UIControlStateNormal];
    [pwdbtn addTarget:self action:@selector(changerPwdState:) forControlEvents:UIControlEventTouchUpInside];
    [usepwdView addSubview:pwdbtn];
    _userPwd = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(pwdbtn.frame)+10, 0, usepwdView.frame.size.width-pwdbtn.frame.size.width-25, heigth)];
    _userPwd.placeholder = @"请输入登陆密码";
    _userPwd.clearButtonMode = UITextFieldViewModeWhileEditing;
    _userPwd.delegate = self;
    _userPwd.secureTextEntry = YES;
    [usepwdView addSubview:_userPwd];
    [self addSubview:usepwdView];
    
    //username
    
    UIView *usenameView = [[UIView alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(usepwdView.frame)-10-heigth*2, [UIUTils getWindowWidth]-20*2, heigth)];
    usenameView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.3];
    usenameView.layer.cornerRadius = heigth/2.0;
    [self addSubview:usenameView];
    
    UIImageView *userimg = [[UIImageView alloc] initWithFrame:CGRectMake(12, 10, heigth-10*2, heigth-10*2)];
    userimg.image = [UIImage imageNamed:@"usename"];
    [usenameView addSubview:userimg];
    
    _userName = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(userimg.frame)+10, 0, usenameView.frame.size.width-userimg.frame.size.width-25, heigth)];
    _userName.placeholder = @"请输入登陆手机号";
    _userName.clearButtonMode = UITextFieldViewModeWhileEditing;
    _userName.delegate = self;
    [usenameView addSubview:_userName];
    
    //logo
    _logoimageView = [[UIImageView alloc] initWithFrame:CGRectMake(([UIUTils getWindowWidth]-(CGRectGetMaxY(usenameView.frame)-heigth-20*2-20))/2, 20+20, (CGRectGetMaxY(usenameView.frame)-heigth-20*2-20), (CGRectGetMaxY(usenameView.frame)-heigth-20*2-20))];
    _logoimageView.layer.cornerRadius = (CGRectGetMaxY(usenameView.frame)-heigth-20*2-20)/2.0;
    _logoimageView.backgroundColor = [UIColor whiteColor];
//    _logoimageView.image = [UIImage imageNamed:@""];
    [self addSubview:_logoimageView];
    
    //登录
    _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _loginButton.frame = CGRectMake(20, self.center.y+10, [UIUTils getWindowWidth]-20*2, heigth);
    [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [_loginButton addTarget:self action:@selector(loginButtonPress) forControlEvents:UIControlEventTouchUpInside];
    _loginButton.layer.cornerRadius = 10;
    _loginButton.backgroundColor = [UIUTils colorFromHexCode:@"#eb6065"];
    [self addSubview:_loginButton];
    
    
    //忘记密码
    
    _forgetButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _forgetButton.frame = CGRectMake(30, CGRectGetMaxY(_loginButton.frame)+10, ([UIUTils getWindowWidth]-30*2)/2.0, heigth-10);
    [_forgetButton setTitle:@"忘记密码？" forState:UIControlStateNormal];
     _forgetButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//    _forgetButton.backgroundColor = [UIColor redColor];
    _forgetButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    [_forgetButton addTarget:self action:@selector(forgetButtonPress) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_forgetButton];
    
    
    //注册新用户
    _registButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _registButton.frame = CGRectMake(CGRectGetMaxX(_forgetButton.frame), CGRectGetMaxY(_loginButton.frame)+10, ([UIUTils getWindowWidth]-30*2)/2.0, heigth-10);
//    _registButton.backgroundColor = [UIColor yellowColor];
    [_registButton setTitle:@"新用户注册" forState:UIControlStateNormal];
    _registButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    _registButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    [_registButton addTarget:self action:@selector(registbuttonPress) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_registButton];
    
    
}

#pragma mark UITextFieldDelegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark UIActionSheetDelegate

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"####%ld",(long)buttonIndex);
    
    switch (buttonIndex) {
        case 0:
        {
            if (self.delegate&&[self.delegate respondsToSelector:@selector(loginViewforgetpwdButtonPress)]) {
                [self.delegate loginViewforgetpwdButtonPress];
            }
        }
            break;
        case 1:
        {
            if (self.delegate&&[self.delegate respondsToSelector:@selector(loginViewsmsButtonPress)]) {
                [self.delegate loginViewsmsButtonPress];
            }
        }
            break;
        case 2:
            
            break;
            
        default:
            break;
    }
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}

//输入密码时可以更换输入密码的状态
- (void)changerPwdState:(UIButton *)btn
{
    if (_userPwd.secureTextEntry == YES) {
        [btn setBackgroundImage:[UIImage imageNamed:@"password_one"] forState:UIControlStateNormal];
        _userPwd.secureTextEntry = NO;
    }
    else
    {
        [btn setBackgroundImage:[UIImage imageNamed:@"password"] forState:UIControlStateNormal];
        _userPwd.secureTextEntry = YES;
    }
}

#pragma mark 代理方法

- (void)loginButtonPress
{
    [self endEditing:YES];
    if (self.delegate&&[self.delegate respondsToSelector:@selector(loginViewLoginButtonPressWithUsername:UserPwd:)]) {
        [self.delegate loginViewLoginButtonPressWithUsername:_userName.text UserPwd:_userPwd.text];
    }
}

- (void)registbuttonPress
{
    [self endEditing:YES];
    if (self.delegate&&[self.delegate respondsToSelector:@selector(loginViewRegistButtonPress)]) {
        [self.delegate loginViewRegistButtonPress];
    }
}

- (void)forgetButtonPress
{
    [self endEditing:YES];
    
    UIActionSheet *sheetView = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"找回密码",@"短信验证登录", nil];
    [sheetView showInView:self];
    
}

@end
